package com.itboy.mall.pms.brand.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import com.itboy.mall.pms.categorybrand.domain.MallCategoryBrand;
import com.itboy.mall.pms.categorybrand.service.IMallCategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.pms.brand.bo.MallBrandAddBo;
import com.itboy.mall.pms.brand.bo.MallBrandQueryBo;
import com.itboy.mall.pms.brand.bo.MallBrandEditBo;
import com.itboy.mall.pms.brand.domain.MallBrand;
import com.itboy.mall.pms.brand.mapper.MallBrandMapper;
import com.itboy.mall.pms.brand.vo.MallBrandVo;
import com.itboy.mall.pms.brand.service.IMallBrandService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 品牌Service业务层处理
 *
 * @author itboy
 * @date 2021-07-13
 */
@Service
public class MallBrandServiceImpl extends ServicePlusImpl<MallBrandMapper, MallBrand> implements IMallBrandService {

	@Autowired
	private IMallCategoryBrandService iMallCategoryBrandService;
    @Override
    public MallBrandVo queryById(String brandId){
        return getVoById(brandId, MallBrandVo.class);
    }

    @Override
    public TableDataInfo<MallBrandVo> queryPageList(MallBrandQueryBo bo) {
        PagePlus<MallBrand, MallBrandVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallBrandVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallBrandVo> queryList(MallBrandQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallBrandVo.class);
    }

    private LambdaQueryWrapper<MallBrand> buildQueryWrapper(MallBrandQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallBrand> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), MallBrand::getName, bo.getName());
        lqw.like(StrUtil.isNotBlank(bo.getDescript()), MallBrand::getDescript, bo.getDescript());
        lqw.eq(bo.getShowStatus() != null, MallBrand::getShowStatus, bo.getShowStatus());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallBrandAddBo bo) {
        MallBrand add = BeanUtil.toBean(bo, MallBrand.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
	@Transactional(rollbackFor = Exception.class)
    public Boolean updateByEditBo(MallBrandEditBo bo) {
        MallBrand update = BeanUtil.toBean(bo, MallBrand.class);
		//修改关联分类中冗字段
        LambdaQueryWrapper<MallCategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(MallCategoryBrand::getBrandId,update.getBrandId());
		List<MallCategoryBrand> mallCategoryBrands = iMallCategoryBrandService.list(queryWrapper);
		if(CollectionUtils.isNotEmpty(mallCategoryBrands)){
			mallCategoryBrands.forEach(e->{
				e.setBrandName(update.getName());
			});
			iMallCategoryBrandService.updateBatchById(mallCategoryBrands);
		}
		validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallBrand entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
